package com.zouchao.module.module_project_main.fragment;

import android.annotation.SuppressLint;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.zouchao.module.common.base.BaseFragment;
import com.zouchao.module.common.util.DensityUtil;
import com.zouchao.module.common.util.LogUtils;
import com.zouchao.module.common.utils.ToastUtils;
import com.zouchao.module.module_api.api.one.RetrofitUtil;
import com.zouchao.module.module_api.bean.DataList;
import com.zouchao.module.module_api.bean.list.Data;
import com.zouchao.module.module_project_main.R;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 展现内容list的fragment，可创建类型：Read、Music、movie
 * Created by 刘晓彬 on 2017/3/27.
 */

public class ListFragment extends BaseFragment {
    public static final int TYPE_READ = 0;
    public static final int TYPE_MUSIC = 1;
    public static final int TYPE_MOVIE = 2;

    private int mType;

    private RecyclerView mRecyclerView;
    private ImageView mIvLoading;

    public ListFragment() {}

    @SuppressLint("ValidFragment")
    public ListFragment(int type) {
        mType = type;
    }

    protected int getRootViewId() {
        return R.layout.fragment_content;
    }

    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fetchData();
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mRootView == null) {
            mRootView = inflater.inflate(getRootViewId(), null);
            initView(mRootView);
            initData();
        }
        LogUtils.d("会反复执行onCreateView不可能吧");
        return mRootView;
    }
    protected void initData() {
        toggleLoading(true);
        ((AnimationDrawable) mIvLoading.getDrawable()).start();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
    }
    protected void toggleLoading(boolean isShow) {
        if (mIvLoading != null)
            mIvLoading.setVisibility(View.VISIBLE);
        else    mIvLoading.setVisibility(View.GONE);
    }
    protected void initView(View rootView) {
        rootView.setPadding(0, DensityUtil.dip2px(50), 0, 0);
        mIvLoading = (ImageView) rootView.findViewById(R.id.iv_loading);
        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
    }

    private void fetchData() {
        Callback<DataList> callback = new Callback<DataList>() {
            @Override
            public void onResponse(Call<DataList> call, Response<DataList> response) {
                Data data = new Data();
                data.content_list = response.body().data;
//                mRecyclerView.setAdapter(new ListRvAdapter(getActivity(), data));
                toggleLoading(false);
            }

            @Override
            public void onFailure(Call<DataList> call, Throwable t) {
                ToastUtils.showLongToast("失败，再次链接");
                fetchData();
            }
        };

        Call<DataList> call;
        switch (mType) {
            case TYPE_READ:
                call = RetrofitUtil.getReadListCall();
                break;
            case TYPE_MUSIC:
                call = RetrofitUtil.getMusicListCall();
                break;
            case TYPE_MOVIE:
                call = RetrofitUtil.getMovieListCall();
                break;
            default:
                ToastUtils.showLongToast("Error:ListFragment type error");
                return;
        }
        call.enqueue(callback);
    }

}